<template>
	<el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
		:size="formSize" status-icon>
		<el-form-item label="字典名称" prop="TypeCode">
			<el-input v-model="ruleForm.TypeCode" />
		</el-form-item>
		<el-form-item label="字典值" prop="TypeName">
			<el-input v-model="ruleForm.TypeName" autocomplete="off" />
		</el-form-item>
		<el-form-item label="描述" prop="TypeDesc">
			<el-input v-model="ruleForm.TypeDesc" />
		</el-form-item>
		<el-form-item label="状态" prop="Status">
			<el-select-v2 v-model="ruleForm.Status" placeholder="请选择状态" :options="optionsStatus" />
		</el-form-item>

		<el-form-item>
			<el-button type="primary" @click="submitForm(ruleFormRef)">
				保存
			</el-button>
			<el-button @click="resetForm(ruleFormRef)">重置</el-button>
		</el-form-item>
	</el-form>

</template>
<script lang="ts" setup>
	import { reactive, ref, onMounted } from 'vue'
	import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
	import { ElNotification } from 'element-plus'
	import approvalflow from "../../api/wf_approvalflowapi"

	const props = defineProps({
		Id: Number,
	});
	const emit = defineEmits(['onSave'])

	interface RuleForm {
		TypeCode : string
		TypeName : string
		TypeDesc : string
        Status :number
	}

	const formSize = ref<ComponentSize>('default')
	const ruleFormRef = ref<FormInstance>()
	const ruleForm = ref<RuleForm>({
			TypeCode: '',
            TypeName: '',
            TypeDesc: '',
			Status: 1,
		
	})

	const rules = reactive<FormRules<RuleForm>>({
		TypeName: [
			{ required: true, message: '请输入字典名称', trigger: 'blur' },
			{ min: 2, max: 5, message: '2-5个字符', trigger: 'blur' },
		],
		

	})

	const optionsStatus = [
		{
			value: 1,
			label: "正常",
		},
		{
			value: 0,
			label: "删除",
		},
		{
			value: -1,
			label: "禁用",
		}
	]

	const getData = () => {
		approvalflow.get(props.Id).then(res => {
			console.log("aaa=", res.data);
			ruleForm.value = res.data
		})
	}
	onMounted(() => {
		getData();
	})
	const submitForm = async (formEl : FormInstance | undefined) => {
		if (!formEl) return
		await formEl.validate((valid, fields) => {
			if (valid) {
				console.log("ruleForm.data", ruleForm.value)

				approvalflow.put(ruleForm.value).then((res:any) => {
					if (res.code == 1) {
						ElNotification({
							title: 'Success',
							message: '修改成功',
							type: 'success',
							duration: 800,
							onClose: () => {
								emit("onSave", true)
							}
						})
					} else {
						ElNotification({
							title: 'Error',
							message: '失败',
							type: 'error',
							duration: 800,
						})
					}
				}).catch(err => {
					ElNotification({
						title: 'Error',
						message: '失败',
						type: 'error',
						duration: 800,
					})
				})
			} else {
				console.log('error submit!', fields)
			}
		})
	}

	const resetForm = (formEl : FormInstance | undefined) => {
		if (!formEl) return
		formEl.resetFields()
	}
</script>

<style>
</style>